﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IXSHTDAL : IDAL<XSHT>
    {
        List<vXSHT> GetXSHT(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }

    public class XSHTDAL : BaseDAL<XSHT>, IXSHTDAL
    {
        public XSHTDAL(IUnitOfWork context)
            : base(context)
        {
        }

        public List<vXSHT> GetXSHT(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = dataContext.vXSHT.AsQueryable();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("ID") && !string.IsNullOrEmpty(dc["ID"]))
                {
                    Guid keyVal = Guid.Parse(dc["ID"]);
                    result = result.Where(t => t.ID == keyVal);
                }
                if (dc.ContainsKey("HTZT") && !string.IsNullOrEmpty(dc["HTZT"]))
                {
                    int keyVal = Convert.ToInt32(dc["HTZT"]);
                    result = result.Where(t => t.HTZT == keyVal);
                }
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                {
                    string keyVal = dc["BH"];
                    result = result.Where(t => t.BH.Contains(keyVal));
                }
                if (dc.ContainsKey("KHMC") && !string.IsNullOrEmpty(dc["KHMC"]))
                {
                    string keyVal = dc["KHMC"];
                    result = result.Where(t => t.KHJC.Contains(keyVal));
                }
                if (dc.ContainsKey("Department") && !string.IsNullOrEmpty(dc["Department"]))
                {
                    Guid? keyVal = Guid.Parse(dc["Department"]);
                    result = result.Where(t => t.XSBMID == keyVal);
                }
                if (dc.ContainsKey("Staff") && !string.IsNullOrEmpty(dc["Staff"]))
                {
                    Guid? keyVal = Guid.Parse(dc["Staff"]);
                    result = result.Where(t => t.XSRYID == keyVal);
                }
                if (dc.ContainsKey("DHSJ1") && !string.IsNullOrEmpty(dc["DHSJ1"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["DHSJ1"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.DHSJ >= keyVal);
                }
                if (dc.ContainsKey("DHSJ2") && !string.IsNullOrEmpty(dc["DHSJ2"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["DHSJ2"]).AddDays(1).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.DHSJ <= keyVal);
                }
                if (dc.ContainsKey("CHSJ1") && !string.IsNullOrEmpty(dc["CHSJ1"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CHSJ1"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.CHSJ >= keyVal);
                }
                if (dc.ContainsKey("CHSJ2") && !string.IsNullOrEmpty(dc["CHSJ2"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CHSJ2"]).AddDays(1).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.CHSJ <= keyVal);
                }
            }
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                pageIndex = 0;
            else
                pageIndex--;
            return result.Skip(Convert.ToInt32(pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
